<template>
  <div class="login-container">
    <div class="login-content">
      <div class="login-left">
        <div class="login-box">
          <div class="login-header">
            <img src="@/assets/logo.png" alt="振涛教育" class="logo-image">
            <div class="title">振涛教育登录平台</div>
          </div>

          <el-form
              ref="ruleFormRef"
              :model="ruleForm"
              :rules="rules"
              class="login-form"
          >
            <el-form-item prop="username">
              <el-input
                  v-model="ruleForm.username"
                  placeholder="请输入用户名"
                  prefix-icon="User"
              />
            </el-form-item>

            <el-form-item prop="password">
              <el-input
                  v-model="ruleForm.password"
                  type="password"
                  placeholder="请输入密码"
                  prefix-icon="Lock"
                  show-password
              />
            </el-form-item>

<!--            <div class="verify-code">-->
<!--              <el-input-->
<!--                placeholder="验证码"-->
<!--                class="verify-input"-->
<!--              />-->
<!--              <img src="验证码图片URL" alt="验证码" class="code-image">-->
<!--            </div>-->

            <div class="remember-section">
              <el-checkbox v-model="rememberMe">记住密码</el-checkbox>
              <a href="#" class="forgot-link">忘记密码？</a>
            </div>

            <el-button
                type="primary"
                class="login-button"
                @click="submitForm"
                :loading="isLoading"
            >
              {{ isLoading ? '登录中...' : '登录' }}
            </el-button>
          </el-form>

          <div class="login-footer">
            <a href="#" class="help-link">安全登录帮助</a>
          </div>
        </div>
      </div>
      <div class="login-right">
        <div class="campus-image"></div>
        <div class="wave-shape"></div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { login } from '@/assets/user.js'

const ruleFormRef = ref()
const ruleForm = ref({
  username: '',
  password: ''
})

const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
})

const isLoading = ref(false)
const rememberMe = ref(false)

const submitForm = async () => {
  if (!ruleFormRef.value) return
  const valid = await ruleFormRef.value.validate()
  if (!valid) return

  isLoading.value = true
  try {
    const res = await login(ruleForm.value)
    if (res.data.code === 200) {
      if (rememberMe.value) {
        localStorage.setItem('rememberMe', 'true')
      } else {
        localStorage.removeItem('rememberMe')
      }
      ElMessage.success('登录成功')
      localStorage.setItem('username',res.data.data.username)
      if (res.data.data.rname=="导员"){
        localStorage.setItem("counselorName",res.data.data.tbCounselor.counselorName)
        localStorage.setItem("classId",res.data.data.tbCounselor.classId)
      }
      if (res.data.data.rname=="讲师"){
        localStorage.setItem("teacherName",res.data.data.tbTeacher.teacherName)
        localStorage.setItem("classId",res.data.data.tbTeacher.classId)
      }
      if (res.data.data.rname=="主任" && res.data.data.tbTeacher!=null){
        localStorage.setItem("teacherName",res.data.data.tbTeacher.teacherName)
        localStorage.setItem("classId",res.data.data.tbTeacher.classId)
      }
      if (res.data.data.rname=="主任" && res.data.data.tbCounselor!=null){
        localStorage.setItem("counselorName",res.data.data.tbCounselor.counselorName)
        localStorage.setItem("classId",res.data.data.tbCounselor.classId)
      }
      localStorage.setItem('role',res.data.data.rname)

      setTimeout(() => {
        window.location.href = '/main'
      }, 1000)
    } else {
      ElMessage.error(res.data.message || '登录失败，请重试')
    }
  } catch (error) {
    console.error('登录请求失败', error)
    ElMessage.error('网络错误，请稍后再试')
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped>
.login-container {
  width: 100%;
  min-height: 100vh;
  background: #f5f7fa;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-content {
  width: 100%;
  max-width: 1200px;
  min-height: 600px;
  height: 85vh;
  background: #fff;
  border-radius: 20px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
  display: flex;
  overflow: hidden;
}

.login-left {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 60px;
  position: relative;
}

.login-left::after {
  content: '';
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 1px;
  height: 70%;
  background: linear-gradient(180deg, 
    transparent 0%, 
    rgba(0, 0, 0, 0.05) 50%,
    transparent 100%
  );
}

.login-box {
  width: 100%;
  max-width: 360px;
}

.login-header {
  text-align: center;
  margin-bottom: 48px;
}

.logo-image {
  width: 160px;
  height: auto;
  margin-bottom: 24px;
}

.title {
  font-size: 28px;
  color: #1a1a1a;
  font-weight: 600;
  margin-bottom: 12px;
  letter-spacing: 1px;
}

.title::after {
  content: '';
  display: block;
  width: 40px;
  height: 3px;
  background: #409EFF;
  margin: 12px auto 0;
  border-radius: 2px;
}

.login-form {
  .el-input {
    --el-input-height: 46px;
    margin-bottom: 24px;
  }

  :deep(.el-input__wrapper) {
    background-color: #f7f8fa;
    border: 1px solid transparent;
    border-radius: 8px;
    box-shadow: none;
    transition: all 0.3s ease;

    &:hover {
      border-color: #409EFF;
      background-color: #fff;
    }

    &.is-focus {
      border-color: #409EFF;
      background-color: #fff;
      box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.1);
    }
  }

  :deep(.el-input__inner) {
    color: #333;
    font-size: 15px;

    &::placeholder {
      color: #999;
    }
  }
}

.remember-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 8px 0 28px;
  color: #666;
}

.forgot-link {
  color: #409EFF;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s ease;

  &:hover {
    color: #66b1ff;
  }
}

.login-button {
  width: 100%;
  height: 46px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  background: linear-gradient(135deg, #409EFF 0%, #3a8ee6 100%);
  border: none;
  border-radius: 8px;
  margin-bottom: 24px;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
    background: linear-gradient(135deg, #66b1ff 0%, #409EFF 100%);
  }

  &:active {
    transform: translateY(0);
  }
}

.login-footer {
  text-align: center;
}

.help-link {
  color: #666;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s ease;

  &:hover {
    color: #409EFF;
  }
}

.login-right {
  flex: 1.2;
  position: relative;
  overflow: hidden;
}

.campus-image {
  position: absolute;
  top: 0;
  right: 0;
  width: 100%;
  height: 100%;
  background-image: url('@/assets/campus.jpg');
  background-size: cover;
  background-position: center;
  transition: transform 0.3s ease;

  &:hover {
    transform: scale(1.02);
  }
}

.wave-shape {
  position: absolute;
  left: -80px;
  top: 0;
  bottom: 0;
  width: 120px;
  background: #fff;
  filter: drop-shadow(2px 0 6px rgba(0, 0, 0, 0.03));
  clip-path: path('M100,0 C120,30 120,70 100,100 L0,100 L0,0 Z');
}

@media (max-width: 1200px) {
  .login-content {
    height: 100vh;
    max-width: none;
    border-radius: 0;
  }
}

@media (max-width: 768px) {
  .login-content {
    flex-direction: column;
  }

  .login-left {
    padding: 40px 20px;
  }

  .login-right {
    display: none;
  }

  .login-left::after {
    display: none;
  }
}
</style>